package com.hns.can.service.service;

import com.hns.can.base.entity.SysUserEntity;
import com.hns.can.service.entity.BaseDrvCommentEntity;
import com.hns.can.service.vo.DriverCommentParamsVo;
import com.hns.can.service.vo.DriverCommentVo;
import com.hns.frame.service.CommonService;
import com.hns.tool.pub.DataUtil;
import com.hns.tool.pub.PubUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.MessageFormat;
import java.util.*;

/**
 * 驾驶员评语Service
 * @author:zhangzq
 * @email:
 * @version Revision 2.0.0
 */
@Service
public class DriverCommentService extends CommonService {
    public String getCommentGrade4Bhv(DriverCommentParamsVo paramsVo) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("corpId", paramsVo.getCorpId());
        paramMap.put("commentTypeCode", "CMT00000");
        paramMap.put("codeValue", "CMT00001");
        paramMap.put("compareValue", paramsVo.getCompareValue());
        paramMap.put("lineId", paramsVo.getLineId());
        String grade = (String) this.sqlMapDao.selectOne("driverComment.getCommentGrade", paramMap);
        return grade;
    }

    /**
     *
     * @return 评语等级
     * @author:Chenwm
     * @email:112362667@qq.com
     * @创建日期:2014-9-16
     * @功能说明:取得节能行为评语等级
     */
    public String getCommentGrade4Ena(DriverCommentParamsVo paramsVo) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("corpId", paramsVo.getCorpId());
        paramMap.put("commentTypeCode", "CMT00000");
        paramMap.put("codeValue", "CMT00002");
        paramMap.put("compareValue", paramsVo.getCompareValue());
        paramMap.put("lineId", paramsVo.getLineId());
        String grade = (String) this.sqlMapDao.selectOne("driverComment.getCommentGrade", paramMap);
        return grade;
    }

    /**
     *
     * @return 评语等级
     * @author:Chenwm
     * @email:112362667@qq.com
     * @创建日期:2014-9-16
     * @功能说明:取得驾驶员情绪评语等级
     */
    public String getCommentGrade4Mood(DriverCommentParamsVo paramsVo) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("corpId", paramsVo.getCorpId());
        paramMap.put("commentTypeCode", "CMT00000");
        paramMap.put("codeValue", "CMT00003");
        paramMap.put("compareValue", paramsVo.getCompareValue());
        paramMap.put("lineId", paramsVo.getLineId());
        String grade = (String) this.sqlMapDao.selectOne("driverComment.getCommentGrade", paramMap);
        return grade;
    }

    /**
     *
     * @return 评语等级
     * @author:qianjy
     * @email:494758538@qq.com
     * @创建日期:2014-11-27
     * @功能说明:取得驾驶员安保评语等级
     */
    public String getCommentGrade4Safe(DriverCommentParamsVo paramsVo) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("corpId", paramsVo.getCorpId());
        paramMap.put("commentTypeCode", "CMT00000");
        paramMap.put("codeValue", "CMT00004");
        paramMap.put("compareValue", paramsVo.getCompareValue());
        paramMap.put("lineId", paramsVo.getLineId());
        String grade = (String) this.sqlMapDao.selectOne("driverComment.getCommentGrade", paramMap);
        return grade;
    }

    /**
     *
     * @return 评语内容
     * @author:Chenwm
     * @email:112362667@qq.com
     * @创建日期:2014-9-16
     * @功能说明:取得安全行为评语内容
     */
    public String getCommentContent4Bhv(DriverCommentParamsVo paramsVo) {
        String content = "";

        // 1.取得等级评语
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("corpId", paramsVo.getCorpId());
        paramMap.put("commentTypeCode", "CMT00000");
        paramMap.put("codeValue", "CMT00001");
        paramMap.put("compareValue", paramsVo.getCompareValue());
        paramMap.put("lineId", paramsVo.getLineId());
        String tmpContent = (String) this.sqlMapDao.selectOne("driverComment.getCommentContentStr", paramMap);
        String queryTime = paramsVo.getBeginTime();// + " ~ " + paramsVo.getEndTime();
        content = MessageFormat.format(tmpContent, paramsVo.getOrganName(), paramsVo.getLineName(), paramsVo.getDrvName(), queryTime, paramsVo.getCompareValue());

        // 2.取得行为改进评语  [车辆未停稳开车门、车辆起步不关车门、超车、急转弯、空挡滑行、夜间行驶过长]
        paramsVo.setCommentTypeCode("CMT10000");
        content = content + "，" + this.getImproveComment(paramsVo, "1", "2", "3", "4", "5", "8");

        // 3.取得技能改进评语  [倒档时车速超速报警、ABS制动（急刹）、起步急加速]
        paramsVo.setCommentTypeCode("CMT20000");
        content = content + "，" + this.getImproveComment(paramsVo, "6", "7", "9");

        return content;
    }

    /**
     * 取得安全行为改进评语
     * @param paramsVo
     * @param codeValues
     * @return
     */
    private String getImproveComment(DriverCommentParamsVo paramsVo, String... codeValues) {
        List<DriverCommentParamsVo> paramBhvList = new ArrayList<DriverCommentParamsVo>();
        for (String codeValue : codeValues) {
            DriverCommentParamsVo tmpVo = new DriverCommentParamsVo();
            tmpVo.setCorpId(paramsVo.getCorpId());
            tmpVo.setCommentTypeCode(paramsVo.getCommentTypeCode());
            tmpVo.setCodeValue(codeValue);

            if ("1".equals(codeValue) && paramsVo.getBhvCount1() > 0) {
                tmpVo.setCompareValue(new BigDecimal(paramsVo.getBhvCount1()));
                paramBhvList.add(tmpVo);
            } else if ("2".equals(codeValue) && paramsVo.getBhvCount2() > 0) {
                tmpVo.setCompareValue(new BigDecimal(paramsVo.getBhvCount2()));
                paramBhvList.add(tmpVo);
            } else if ("3".equals(codeValue) && paramsVo.getBhvCount3() > 0) {
                tmpVo.setCompareValue(new BigDecimal(paramsVo.getBhvCount3()));
                paramBhvList.add(tmpVo);
            } else if ("4".equals(codeValue) && paramsVo.getBhvCount4() > 0) {
                tmpVo.setCompareValue(new BigDecimal(paramsVo.getBhvCount4()));
                paramBhvList.add(tmpVo);
            } else if ("5".equals(codeValue) && paramsVo.getBhvCount5() > 0) {
                tmpVo.setCompareValue(new BigDecimal(paramsVo.getBhvCount5()));
                paramBhvList.add(tmpVo);
            } else if ("6".equals(codeValue) && paramsVo.getBhvCount6() > 0) {
                tmpVo.setCompareValue(new BigDecimal(paramsVo.getBhvCount6()));
                paramBhvList.add(tmpVo);
            } else if ("7".equals(codeValue) && paramsVo.getBhvCount7() > 0) {
                tmpVo.setCompareValue(new BigDecimal(paramsVo.getBhvCount7()));
                paramBhvList.add(tmpVo);
            } else if ("8".equals(codeValue) && paramsVo.getBhvCount8() > 0) {
                tmpVo.setCompareValue(new BigDecimal(paramsVo.getBhvCount8()));
                paramBhvList.add(tmpVo);
            } else if ("9".equals(codeValue) && paramsVo.getBhvCount9() > 0) {
                tmpVo.setCompareValue(new BigDecimal(paramsVo.getBhvCount9()));
                paramBhvList.add(tmpVo);
            }
        }

        Map<String, Object> paramMap = new HashMap<String, Object>();
        String tmpContent = "";
        if (paramBhvList.size() == 0) { // 未发生上述行为
            paramMap.clear();
            paramMap.put("corpId", paramsVo.getCorpId());
            paramMap.put("commentTypeCode", paramsVo.getCommentTypeCode());
            paramMap.put("codeValue", "-1");
            paramMap.put("compareValue", null);
            paramMap.put("lineId", paramsVo.getLineId());
            tmpContent = (String) this.sqlMapDao.selectOne("driverComment.getCommentContentStr", paramMap);
        } else {
            paramMap.clear();
            paramMap.put("commentList", paramBhvList);
            paramMap.put("lineId", paramsVo.getLineId());
            List queryResultList = this.sqlMapDao.selectList("driverComment.getCommentContentMap", paramMap);
            for (int i = 0; i < queryResultList.size(); i++) {
                Map<String, Object> map = (Map<String, Object>) queryResultList.get(i);
                tmpContent = tmpContent + MessageFormat.format((String)map.get("COMMENT_CNTNT"), map.get("CODE_DSC"));
            }
        }

        return tmpContent;
    }

    /**
     *
     * @return 评语等级
     * @author:Chenwm
     * @email:112362667@qq.com
     * @创建日期:2014-9-16
     * @功能说明:取得节能行为评语内容
     */
    public String getCommentContent4Ena(DriverCommentParamsVo paramsVo) {
        String content = "";

        // 取得等级评语
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("corpId", paramsVo.getCorpId());
        paramMap.put("commentTypeCode", "CMT00000");
        paramMap.put("codeValue", "CMT00002");
        paramMap.put("compareValue", paramsVo.getCompareValue());
        paramMap.put("lineId", paramsVo.getLineId());
        String tmpContent = (String) this.sqlMapDao.selectOne("driverComment.getCommentContentStr", paramMap);
        String queryTime = paramsVo.getBeginTime(); //+ " ~ " + paramsVo.getEndTime();
        content = MessageFormat.format(tmpContent, paramsVo.getOrganName(), paramsVo.getLineName(), paramsVo.getDrvName(), queryTime);

        return content;
    }

    /**
     *
     * @return 评语等级
     * @author:Chenwm
     * @email:112362667@qq.com
     * @创建日期:2014-9-16
     * @功能说明:取得驾驶员情绪评语内容
     */
    public String getCommentContent4Mood(DriverCommentParamsVo paramsVo) {
        String content = "";

        // 取得等级评语
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("corpId", paramsVo.getCorpId());
        paramMap.put("commentTypeCode", "CMT00000");
        paramMap.put("codeValue", "CMT00003");
        paramMap.put("compareValue", paramsVo.getCompareValue());
        paramMap.put("lineId", paramsVo.getLineId());
        String tmpContent = (String) this.sqlMapDao.selectOne("driverComment.getCommentContentStr", paramMap);
        String queryTime = paramsVo.getBeginTime().substring(0, 4) + "年度";
        content = MessageFormat.format(tmpContent, paramsVo.getOrganName(),
                paramsVo.getLineName(), paramsVo.getDrvName(), queryTime,
                paramsVo.getCompareValue(), paramsVo.getMoodPercent(),
                paramsVo.getMoodOccurCount(), paramsVo.getMoodOccurTime());

        return content;
    }

    /**
     *
     * @return 评语等级
     * @author:Qianjy
     * @email:494758538@qq.com
     * @创建日期:2014-11-27
     * @功能说明:取得驾驶员安保评语内容
     */
    public String getCommentContent4Safe(DriverCommentParamsVo paramsVo) {
        String content = "";

        // 1.取得等级评语
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("corpId", paramsVo.getCorpId());
        paramMap.put("commentTypeCode", "CMT00000");
        paramMap.put("codeValue", "CMT00004");
        paramMap.put("compareValue", paramsVo.getCompareValue());
        paramMap.put("lineId", paramsVo.getLineId());
        String tmpContent = (String) this.sqlMapDao.selectOne("driverComment.getCommentContentStr", paramMap);
        String queryTime = paramsVo.getBeginTime();
        content = MessageFormat.format(tmpContent, paramsVo.getOrganName(), paramsVo.getLineName(), paramsVo.getDrvName(), queryTime, paramsVo.getCompareValue());

        // 2.取得事故改进评语
        paramsVo.setCommentTypeCode("CMT30000");
        content = content + "，" + this.getSafeImproveComment(paramsVo, "1");

        // 3.取得违章改进评语
        paramsVo.setCommentTypeCode("CMT40000");
        content = content + "，" + this.getSafeImproveComment(paramsVo, "2");

        return content;
    }

    /**
     * 取得安全行为改进评语
     * @param paramsVo
     * @param codeValues
     * @return
     */
    private String getSafeImproveComment(DriverCommentParamsVo paramsVo, String... codeValues) {
        List<DriverCommentParamsVo> paramBhvList = new ArrayList<DriverCommentParamsVo>();
        for (String codeValue : codeValues) {
            DriverCommentParamsVo tmpVo = new DriverCommentParamsVo();
            tmpVo.setCorpId(paramsVo.getCorpId());
            tmpVo.setCommentTypeCode(paramsVo.getCommentTypeCode());
            tmpVo.setCodeValue(codeValue);

            if ("1".equals(codeValue) && paramsVo.getSafeCount1() > 0) {
                tmpVo.setCompareValue(new BigDecimal(paramsVo.getSafeCount1()));
                paramBhvList.add(tmpVo);
            } else if ("2".equals(codeValue) && paramsVo.getSafeCount2() > 0) {
                tmpVo.setCompareValue(new BigDecimal(paramsVo.getSafeCount2()));
                paramBhvList.add(tmpVo);
            }
        }

        Map<String, Object> paramMap = new HashMap<String, Object>();
        String tmpContent = "";
        if (paramBhvList.size() == 0) { // 未发生上述行为
            paramMap.clear();
            paramMap.put("corpId", paramsVo.getCorpId());
            paramMap.put("commentTypeCode", paramsVo.getCommentTypeCode());
            paramMap.put("codeValue", "-1");
            paramMap.put("compareValue", null);
            paramMap.put("lineId", paramsVo.getLineId());
            tmpContent = (String) this.sqlMapDao.selectOne("driverComment.getCommentContentStr", paramMap);
        } else {
            paramMap.clear();
            paramMap.put("commentList", paramBhvList);
            paramMap.put("lineId", paramsVo.getLineId());
            List queryResultList = this.sqlMapDao.selectList("driverComment.getCommentContentMap", paramMap);
            for (int i = 0; i < queryResultList.size(); i++) {
                Map<String, Object> map = (Map<String, Object>) queryResultList.get(i);
                tmpContent = tmpContent + MessageFormat.format((String) map.get("COMMENT_CNTNT"), map.get("CODE_DSC"));
            }
        }

        return tmpContent;
    }

    /**
     * @param paraMap
     * @return
     * @author:JeffXu
     * @email:835264047@qq.com
     * @创建日期:2014-11-18
     * @功能说明：安全行为评语设置 - 获取安全行为评语设置信息
     */
    @SuppressWarnings("unchecked")
    @Transactional
    public List<BaseDrvCommentEntity> searchDrvCommentInfo(Map<String, Object> paraMap) {
        List<BaseDrvCommentEntity> list = PubUtil.convertListMapToObject(
                this.sqlMapDao.selectList("driverComment.searchCommentInfo", paraMap), BaseDrvCommentEntity.class);
        return list;
    }

    /**
     * @param data
     * @throws Exception
     * @author:JeffXu
     * @email:835264047@qq.com
     * @创建日期:2014-11-18
     * @功能说明：安全行为评语设置 - 修改安全行为评语设置信息
     */
    @Transactional
    public void uptDrvCommentInfo(DriverCommentVo data) throws Exception {
        String seleId = data.getSeleId();
        String currId = data.getCurrId();
        String corpId = data.getCorpId();
        List<String> commentIds = data.getCommentIds();
        List<String> commentGradeDscs = data.getCommentGradeDscs();
        List<String> commentCntnts = data.getCommentCntnts();
        List<String> remarks = data.getRemarks();
        List<BigDecimal> beginValues = data.getBeginValues();
        List<BigDecimal> endValues = data.getEndValues();

        SysUserEntity user = this.getCurrentUser().getUser();
        if (seleId.equals(currId)) { // 更新已有的数据
            for(int i = 0; i < commentIds.size(); i++){
                BaseDrvCommentEntity entity = this.commonEntityDao.find(commentIds.get(i), BaseDrvCommentEntity.class);
                entity.setCommentGradeDsc(commentGradeDscs.get(i));
                entity.setCommentCntnt(commentCntnts.get(i));
                entity.setBeginValue(beginValues.get(i));
                entity.setEndValue(endValues.get(i));
                entity.setRemark(remarks.get(i));
                entity.setUpdateTime(new Date());
                entity.setUpdateUser(user.getAcctName());
                this.commonEntityDao.update(entity);
            }
        } else { // 添加新数据
            for(int i = 0; i < commentIds.size(); i++){
                BaseDrvCommentEntity entity = this.commonEntityDao.find(commentIds.get(i), BaseDrvCommentEntity.class);
                BaseDrvCommentEntity dbentity = new BaseDrvCommentEntity();
                dbentity.setCommentId(DataUtil.sequenceID());
                PubUtil.copyPropertiesInclude(entity, dbentity, new String[]{"paramCode","commentCntntCode","codeDsc","codeValue","commentTypeName","commentTypeCode","commentGrade"});
                dbentity.setOrganId(seleId);
                dbentity.setCommentGradeDsc(commentGradeDscs.get(i));
                dbentity.setCommentCntnt(commentCntnts.get(i));
                dbentity.setBeginValue(beginValues.get(i));
                dbentity.setEndValue(endValues.get(i));
                dbentity.setRemark(remarks.get(i));
                dbentity.setCrtTime(new Date());
                dbentity.setCrtUser(user.getAcctName());
                dbentity.setUpdateTime(new Date());
                dbentity.setUpdateUser(user.getAcctName());
                dbentity.setCorpId(corpId);
                this.commonEntityDao.save(dbentity);
            }
        }
    }

}